package com.hxkj.cst.chengshuitong.ui.adapter

import android.content.Context
import android.support.annotation.ArrayRes
import android.support.annotation.ColorRes
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.hxkj.cst.chengshuitong.R
import com.hxkj.cst.chengshuitong.util.LogUtils

/**
 * 作者：LuoYang
 * 时间：2017/6/2:10:09
 * 邮箱：18981443691@163.com
 * 说明：
 */
class RatePaymentTableAdapter(val context: Context,
                              val contentArrays: SparseArray<String>,
                              @ArrayRes arrays: Int) : RecyclerView.Adapter<RatePaymentTableAdapter.ViewHolder>() {
    var describeArrays: SparseArray<String> = SparseArray()
    var backgroundColor: Int? = 0

    init {
        val typeArray = context.resources.obtainTypedArray(arrays)
        for (index in 0..typeArray.length() - 1) {
            describeArrays.put(index, typeArray.getString(index))
        }
        typeArray.recycle()

        LogUtils.e(describeArrays.size().toString())
        LogUtils.e(contentArrays.size().toString())
    }

    override fun getItemCount(): Int {
        LogUtils.e(contentArrays.size().toString())
        return contentArrays.size()
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_rate_second, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        holder?.mTvDescribe?.text = describeArrays[position]
        holder?.mTvContent?.text = contentArrays[position]
        if (backgroundColor != 0)
            backgroundColor?.let { holder?.mLlContain?.setBackgroundResource(it) }
    }

    fun setBackgroundColor(@ColorRes color: Int) {
        backgroundColor = color
    }

    class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var mTvDescribe: TextView? = null
        var mTvContent: TextView? = null
        var mLlContain: LinearLayout? = null

        init {
            mTvDescribe = itemView?.findViewById(R.id.irs_tv_describe) as TextView?
            mTvContent = itemView?.findViewById(R.id.irs_tv_content) as TextView?
            mLlContain = itemView?.findViewById(R.id.irs_ll_contain) as LinearLayout?
        }
    }
}